package base.check;

import org.apache.commons.lang.StringUtils;

import base.patterns.operation.Function;

public class Check {
	
	public static Function<Void, String> raiseException = new Function<Void, String>() {
		
		@Override
		public Void call(String msg) {
			throw new CheckException(msg);
		}
	};
	
	public static void isTrue(boolean b, Function<Void, String> checkFailed, String errorMsg) {
		if (!b){
			checkFailed.call(errorMsg);
		}
	}
	
	public static void isFalse(boolean b, Function<Void, String> checkFailed, String errorMsg) {
		if (b){
			checkFailed.call(errorMsg);
		}
	}
	
	public static <T> T notNull(T object, String objectName) {
		Check.isTrue(object != null, raiseException, objectName + " is null");
		return object;
	}
	
	public static void isNull(Object object, String objectName) {
		Check.isTrue(object == null, raiseException, objectName + " is not null");
	}

	public static void isEmpty(String s, String objectName) {
		Check.isTrue(isStringEmpty(s), raiseException, "string " + objectName + " is empty");
	}
	
	public static String isNotEmpty(String s, String objectName) {
		Check.isTrue(!isStringEmpty(s), raiseException, "string " + objectName + " is not empty");
		return s;
	}
	
	private static boolean isStringEmpty(String s){
		return StringUtils.isEmpty(s);
	}
	
	public static void isEqualsStrings(String s1, String s2, String str) {
		Check.notNull(s1, "First parameter isEqualsStrings(s1,s2)");
		Check.notNull(s2, "Two parameter isEqualsStrings(s1,s2)");
		
		Check.isTrue(s1.equals(s2), raiseException, str);
	}
	
	public static void isNotEqualsStrings(String s1, String s2, String str) {
		Check.notNull(s1, "First parameter isEqualsStrings(s1,s2)");
		Check.notNull(s2, "Two parameter isEqualsStrings(s1,s2)");
		
		Check.isFalse(s1.equals(s2), raiseException, str);
	}
	
}
